import React, { Component } from 'react'
import {ListBox} from '../../components'
import { Modal } from 'antd';
import ListMdmaterialmrpForm from './ListMdmaterialmrpForm'
import UpdateMaterialExemptionFormPage from './Update'
import {fetchData} from '../../components/AjaxConsumer'
const confirm = Modal.confirm;

export default class ListMdmaterialmrpPage extends Component {
    constructor(props) {
        super(props);
        this.state={
            loading: false,
            visible: false,
            addFlag: false,
            formData:{},
            submitUrl:'',
        };
        //行操作列表
        this.rowOperation = [

        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'add',
                showName: '新增'
            },{
                handleName: 'edit',
                showName: '修改'
            },{
                handleName: 'delete',
                showName: '删除'
            }
        ];
        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'ListMdmaterialmrpConfig',
            data: [
                {
                    showName: 'materialMrpId',
                    columnName: 'materialMrpId',
                    columnHidden:true,
                    foreverHidden: true
                },{ showName: '工厂',
                    columnName: 'factoryCode',
                    columnWidth: 130
                },{
                    showName: 'MRP',
                    columnName: 'mrpCode',
                    columnWidth: 200
                },{
                    showName: '工序',
                    columnName: 'wkSeq',
                    columnWidth: 200
                },{
                    showName: '拣配交接类型',
                    columnName: 'procLevel',
                    columnWidth: 150,
                    showOption:[
                        {
                            optionKey:'',
                            optionShow:''
                        },{
                            optionKey:'0',
                            optionShow:'人工拣配、人工交接'
                        },{
                            optionKey:'1',
                            optionShow:'自动拣配、人工交接'
                        },{
                            optionKey:'2',
                            optionShow:'人工拣配、自动交接'
                        },{
                            optionKey:'3',
                            optionShow:'自动拣配、自动交接'
                        }
                    ]
                },{
                    showName: '库存地点',
                    columnName: 'sendSpot',
                    columnWidth: 130
                },{
                    showName: '工位',
                    columnName: 'wkposCode',
                    columnWidth: 130
                },{
                    showName: '是否辅料',
                    columnName: 'auxMat',
                    showOption:[//页面显示配置
                        {
                            optionKey:'0',
                            optionShow:'否'
                        },{
                            optionKey:'1',
                            optionShow:'是'
                        }
                    ]
                },{
                    showName: '创建人',
                    columnName: 'createBy'
                },{
                    showName: '创建时间',
                    columnName: 'gmtCreate'
                },{
                    showName: '更新人',
                    columnName: 'lastModifiedBy'
                },{
                    showName: '更新时间',
                    columnName: 'gmtModified'
                }
            ]
        };
        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        this.searchComponentData = [
            {
                showName: '工厂',
                name:'factoryCode',
                type: 'input',
                url: null
            },{
                showName: 'MRP',
                name:'mrpCode',
                type: 'input',
                url: null
            },{
                showName: '工序',
                name:'wkSeq',
                type: 'input',
                url: null
            },{
                showName: '拣配交接类型',
                name:'procLevel',
                type: 'select',
                url: null,
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'人工拣配、人工交接'
                    },{
                        optionKey:'1',
                        optionShow:'自动拣配、人工交接'
                    },{
                        optionKey:'2',
                        optionShow:'人工拣配、自动交接'
                    },{
                        optionKey:'3',
                        optionShow:'自动拣配、自动交接'
                    }
                ]
            },{
                showName: '库存',
                name:'sendSpot',
                type: 'input',
                url: null
            },{
                showName: '工位',
                name:'wkposCode',
                type: 'input',
                url: null
            },{
                showName: '是否辅料',
                name:'auxMat',
                type: 'select',
                url: null,
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'否'
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    }
                ]
            },{
                showName: '创建人',
                name:'createBy',
                type: 'input',
                url: null
            },{
                showName: '创建时间',
                name:'gmtCreate',
                type: 'date',
                url: null
            },{
                showName: '更新人',
                name:'lastModifiedBy',
                type: 'input',
                url: null
            },{
                showName: '更新时间',
                name:'gmtModified',
                type: 'date',
                url: null
            }
        ];

    }

    rowHandles = (strHandles, rowObj) => {
        if(strHandles === 'edit'){
            this.editHandle(rowObj);
        }else if(strHandles === 'delete'){
            this.deleteHandle(rowObj);
        }else if(strHandles === 'add'){
            this.addHandle();
        }
    }

    addHandle = () => {
        this.setState({
            formData:{},
            submitUrl:'/base/data/mdmaterialmrp/c'
        })
        this.showModal();
    }
    editHandle = (rowObj) => {

        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {


            //this.refs.updateDock.editHandle(rowObj[0].data);

            let materialMrpId = rowObj[0].data.materialMrpId;

            let options={
                url:'/base/data/mdmaterialmrp/r/'+materialMrpId,
                type: 'get',
                condition:{}
            };


            fetchData(this, options, (resData) => {
                if (resData.code == '1'){
                    this.setState({
                        formData:resData.data,
                        submitUrl:'/base/data/mdmaterialmrp/u'
                    });
                    this.showModal();
                }else {
                    message.error(resData.msg);
                }
            });
        }

    }

    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            //this.refs.updateDock.deleteHandle(rowObj[0].data)

            console.log('--shanchu---',rowObj);
            let materialMrpId = rowObj[0].data.materialMrpId;
            console.log(materialMrpId);
            let options= {
                url: '/base/data/mdmaterialmrp/d',
                condition:{materialMrpId:materialMrpId}
            };
            let _that = this;
            confirm({
                title: '您确定要删除吗？',
                onOk() {
                    fetchData(_that, options, (resData) => {
                        console.log(resData);
                        _that.setState({
                            resData: resData
                        });

                        _that.refreshHandle();
                        if (resData.code == '1'){
                            message.success('删除成功！');
                            _that.refreshHandle();
                        }else {
                            message.error(resData.msg);
                        }
                    });
                },
                onCancel() {
                    console.log("取消了删除操作!")
                },
            });
        }
    }


    //刷新列表
    refreshHandle = () => {
        this.refs.listMdmaterialmrp.refreshHandle();
    }

    showModal = () => {
        this.setState({
            visible: true
        });
    }

    handleCancel = () => {
        this.setState({
            visible: false
        });
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <div>
                <ListBox
                    ref = 'listMdmaterialmrp'
                    title="特殊物料采购组属性 "
                    isHideLeft={isHideLeft}
                    tableColumnData = {this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url = '/base/data/mdmaterialmrp/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    isRowSingle={true}
                />


                <Modal
                    style={{top: '20px'}}
                    visible={this.state.visible}
                    title=" 采购组是否下线结算"
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                    width={'850px'}
                    footer=''
                    maskClosable={false}>
                    <ListMdmaterialmrpForm
                        onCancel={this.handleCancel}
                        ref="listMdmaterialmrpForm"
                        submitUrl={this.state.submitUrl}
                        formData={this.state.formData}
                        init={this.state.visible}
                        refreshHandle={this.refreshHandle}
                    />
                </Modal>

{/*                <UpdateMaterialExemptionFormPage
                    ref="updateMaterialExemptionFormPage"
                    refreshHandle={this.refreshHandle}
                />*/}

            </div>
        )
    }
}
